package com.ruoyi.web.controller.epm;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.*;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EpmInformation;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 员工信息Controller
 *
 */
@RestController
@RequestMapping("/Empment/information")
public class EpmInformationController extends BaseController
{
    @Autowired
    private IEpmInformationService epmInformationService;
    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private IEpmInformationHistoryService epmInformationHistoryService;
    /**
     * 查询员工信息列表
     */
    @PreAuthorize("@ss.hasPermi('Empment:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(EpmInformation epmInformation)
    {
        startPage();
        List<EpmInformation> list = epmInformationService.selectEpmInformationList(epmInformation);
        return getDataTable(list);
    }

    /**
     * 导出员工信息列表
     */
    @PreAuthorize("@ss.hasPermi('Empment:information:export')")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EpmInformation epmInformation)
    {
        List<EpmInformation> list = epmInformationService.selectEpmInformationList(epmInformation);
        ExcelUtil<EpmInformation> util = new ExcelUtil<EpmInformation>(EpmInformation.class);
        util.exportExcel(response, list, "员工信息数据");
    }

    /**
     * 获取员工信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('Empment:information:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long userId)
    {

        AjaxResult ajax = AjaxResult.success();
        List<SysRole> roles = roleService.selectRoleAll();
        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(userId))
        {
            EpmInformation epm=epmInformationService.selectEpmInformationById(userId);
            ajax.put(AjaxResult.DATA_TAG, epm);
            ajax.put("postIds", postService.selectPostListByUserId(userId));
            ajax.put("roleIds", epm.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        }

        return ajax;
    }



    /**
     * 入职员工
     */
    @PreAuthorize("@ss.hasPermi('Empment:information:add')")
    @Log(title = "员工信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EpmInformation epmInformation)
    {
        epmInformation.setUpdateBy(getUsername());
        return toAjax(epmInformationService.add_epm(epmInformation,getUsername()));
    }

    /**
     * 修改员工信息
     */
    @PreAuthorize("@ss.hasPermi('Empment:information:edit')")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EpmInformation epmInformation)
    {
        epmInformation.setUpdateBy(getUsername());
        return toAjax(epmInformationService.updateEpmInformation(epmInformation));
    }

    /**
     * 删除员工信息
     */
    @PreAuthorize("@ss.hasPermi('Empment:information:remove')")
    @Log(title = "员工信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        if (ArrayUtils.contains(ids, getUserId()))
        {
            return error("当前员工不能删除");
        }
        //todo 需要把所有数据删干净，要检查逻辑
        iSysUserService.deleteUserByIds(ids);
        return toAjax(epmInformationService.deleteEpmInformationByIds(ids));
    }




    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EpmInformation> util = new ExcelUtil<EpmInformation>(EpmInformation.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 导入员工
     */
    @PreAuthorize("@ss.hasPermi('Empment:information:import')")
    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        //todo 导入要加入岗位的导入(岗位导入已完成，下一步需要检查岗位的导出)
        // todo 跑的太慢了，要改进
        ExcelUtil<EpmInformation> util = new ExcelUtil<EpmInformation>(EpmInformation.class);
        List<EpmInformation> EpmInformationList = util.importExcel(file.getInputStream());
        System.out.println(EpmInformationList);
        String message= epmInformationService.importepm(EpmInformationList,getUsername());
        return AjaxResult.success(message);
    }


}
